package com.gin.no_gay_alliance.database.dao;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.RawQuery;
import androidx.sqlite.db.SupportSQLiteQuery;

import com.gin.no_gay_alliance.database.entity.ReplyHistory;

import java.util.Collection;
import java.util.List;

/**
 * 回复历史dao
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/16 14:13
 **/
@Dao
public interface ReplyHistoryDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void add(List<ReplyHistory> replyHistories);

    /**
     * 清空某一主题记录的所有回复
     *
     * @param topicId 主题id
     */
    @Query("delete from ReplyHistory where topicId=:topicId")
    void clear(long topicId);

    /**
     * 某一主题中记录的回复数量
     *
     * @param topicId 主题id
     * @return 回复数量
     */
    @Query("select count(1) from ReplyHistory where topicId=:topicId")
    int count(long topicId);

    /**
     * 动态统计数量
     *
     * @param supportSQLiteQuery 查询条件
     * @return 数量
     */
    @RawQuery
    int count(SupportSQLiteQuery supportSQLiteQuery);

    /**
     * 动态查询历史回复
     *
     * @param supportSQLiteQuery
     * @return 历史回复
     */
    @RawQuery
    List<ReplyHistory> list(SupportSQLiteQuery supportSQLiteQuery);


    /**
     * 按回复id查询回复历史
     *
     * @param replyIds 回复id
     * @param topicId  主题id
     * @return
     */
    @Query("select * from ReplyHistory where replyId in (:replyIds) and topicId=:topicId order by uuid")
    List<ReplyHistory> listByReplyId(Collection<Long> replyIds, long topicId);

    /**
     * 某一主题的最大页数
     *
     * @param topicId 主题id
     * @return 最大页数
     */
    @Query("select max(page) from ReplyHistory where topicId=:topicId")
    int maxPage(long topicId);

    /**
     * 浏览模式的分页查询(包括所有历史版本)
     *
     * @param topicId 主题id
     * @param page    页码
     * @return 回复历史
     */
    @Query("select * from ReplyHistory where topicId=:topicId and floorNumber>=(:page-1)*20 and floorNumber<:page*20 order by uuid")
    List<ReplyHistory> page(long topicId, int page);
}
